﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using HotelRegistrationSystem.DAL;
using HotelRegistrationSystem.Models;

namespace HotelRegistrationSystem.Controllers
{
    [Authorize]
    public class HotelController : Controller
    {
        public void SetRoomViewBag()
        {
            HotelSearchModel model = (HotelSearchModel)Session["SearchSession"];
            ViewBag.RoomTypeId = new SelectList(RoomDAL.GetRoomTypes(), "RoomTypeId", "Type",
                model == null ? null : model.RoomTypeId);
            ViewBag.FacilityId = new SelectList(HotelDAL.GetHotelFacilities(), "FacilityId", "Description",
                model == null ? null : model.FacilityId);
            ViewBag.HotelType = new SelectList(HotelDAL.GetHotelTypes(), "HotelTypeId", "Type",
                model == null ? null : model.HotelTypeId);
            ViewBag.ReservationTypeId = new SelectList(HotelDAL.GetReservationTypes(), "ReservationTypeId", "Type",
                model == null ? null : model.ReservationTypeId);
            ViewBag.HotelId = new SelectList(HotelDAL.GetHotelsRatings(), "HotelId", "Rating",
                model == null ? null : model.HotelId);          
        }

        [AllowAnonymous]
        public ActionResult SearchHotelModalView()
        {
            HotelSearchModel model = (HotelSearchModel)Session["SearchSession"];
            SetHotelSearchModelViewBags(model);
            return PartialView("_SearchInHotels", model);
        }

        private void SetHotelSearchModelViewBags(HotelSearchModel model)
        {
            ViewBag.HotelTypeId = new SelectList(HotelDAL.GetHotelTypes(), "HotelTypeId", "Type",
                model == null ? null : model.HotelTypeId);
            ViewBag.ReservationTypeId = new SelectList(ReservationsDAL.GetReservationTypes(), "ReservationTypeId", "Type",
                model == null ? null : model.ReservationTypeId);
            ViewBag.AddressId = new SelectList(AddressDAL.GetAddressByCities(), "AddressId", "City",
                model == null ? null : model.AddressId);
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Index()
        {
            SetRoomViewBag();
            @ViewBag.IsManagementOn = false;
            List<HotelIndexPageModel> hotels = HotelDAL.GetHotels();
            HotelSearchModel model = new HotelSearchModel();
            var tuple = new Tuple<HotelSearchModel, IEnumerable<HotelIndexPageModel>>(model, hotels);

            return View(tuple);
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Index(HotelSearchModel model)
        {
            SetRoomViewBag();
            @ViewBag.IsManagementOn = false;
            Session["SearchSession"] = model;
            List<HotelIndexPageModel> hotels = HotelDAL.GetHotels(model);

            var tuple = new Tuple<HotelSearchModel, IEnumerable<HotelIndexPageModel>>(model, hotels);

            return View(tuple);
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult ReservationsManagement(int id, int statusId = 0)
        {
            @ViewBag.HotelId = id.ToString();
            @ViewBag.StatusId = statusId.ToString();
            return View();
        }

        [HttpGet]
        public ActionResult MyHotels()
        {
            SetRoomViewBag();
            var user = User.Identity;
            if (user != null)
            {
                @ViewBag.IsManagementOn = true;
                var hotels = HotelDAL.GetHotels().Where(h => h.UserId.Equals(AccountDAL.GetUser(user.Name).UserId));
                HotelSearchModel searchModel = new HotelSearchModel();
                var model = new Tuple<HotelSearchModel, IEnumerable<HotelIndexPageModel>>(searchModel, hotels);

                return View("Index", model);
            }
            else
            {
                return View("Login");
            }
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Details(int id = 0, string date = "")
        {
            ReservationOptions opt = (ReservationOptions)Session["ReservationOptions"];
            if (opt != null && opt.HotelId != id)
            {
                Session["ReservationOptions"] = null;
            }

            Hotel hotel = HotelDAL.GetHotel(id);
            if (hotel == null)
            {
                return HttpNotFound();
            }
            DateTime realDate;
            if (DateTime.TryParse(date, out realDate))
            {
                ViewBag.InitialDate = realDate;
            }
            HashSet<RoomType> roomTypes = new HashSet<RoomType>();
            foreach (Room room in hotel.Rooms)
            {
                roomTypes.Add(room.RoomType); 
            }
            ViewBag.RoomTypes = roomTypes;

            // Get hotel rating
            ViewBag.HotelRating = HotelDAL.GetHotelRating(hotel.HotelId);
            return View(hotel);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Details(int id, FormCollection form, string date = "")
        {
            Hotel hotel;
            
            // Evaluate hotel
            int mark;
            if (HotelDAL.CanEvaluateHotel(id, User.Identity.Name) && int.TryParse(form["ddlMarks"],out mark))
            {
                ViewBag.HotelRating = HotelDAL.EvalHotel(id, mark, User.Identity.Name);
            }

            hotel = HotelDAL.GetHotel(id);
            if (hotel == null)
            {
                return HttpNotFound();
            }           

            DateTime realDate;
            if (DateTime.TryParse(date, out realDate))
            {
                ViewBag.InitialDate = realDate;
            }
            HashSet<RoomType> roomTypes = new HashSet<RoomType>();
            foreach (Room room in hotel.Rooms)
            {
                roomTypes.Add(room.RoomType);
            }
            ViewBag.RoomTypes = roomTypes;

            return View(hotel);
        }

        #region Move to Rooms Controller?!

        [HttpGet]
        [AllowAnonymous]
        public ActionResult LoadReservationView(int id = 0, string date = "")
        {
            RoomReservationModel reservation = new RoomReservationModel();
            reservation.Rooms = RoomDAL.GetAllRoomsByHotel(id);
            if (reservation.Rooms == null)
            {
                return HttpNotFound();
            }

            DateTime realDate;
            if (DateTime.TryParse(date, out realDate))
            {
                ViewBag.SelectedDate = realDate;
            }

            HashSet<RoomTypeModel> roomTypes = new HashSet<RoomTypeModel>();
            foreach (OneRoomReservationModel room in reservation.Rooms)
            {
                roomTypes.Add(room.RoomType);
            }

            ViewBag.RoomTypes = roomTypes;

            ReservationOptions opt = (ReservationOptions)Session["ReservationOptions"];
            if (opt != null)
            {
                foreach (KeyValuePair<int, KeyValuePair<int,bool>> room in opt.ReservedRooms)
                {
                    reservation.Rooms.Where(x => x.RoomId == room.Key).FirstOrDefault().Reserved = room.Value.Value;
                    reservation.Rooms.Where(x => x.RoomId == room.Key).FirstOrDefault().SelectedBedCount = room.Value.Key;
                }

                Session["ReservationOptions"] = null;
            }

            HotelSearchModel model = (HotelSearchModel)Session["SearchSession"];
            if (model != null)
            {
                reservation.GuestsCount = (model.BedsCountMin.HasValue ? model.BedsCountMin.Value : -1);
            }

            reservation.HotelName = HotelDAL.GetHotel(id).Name;
            reservation.HotelId = id;

            return PartialView("_DetailsReservation", reservation);
        }

        [HttpPost]
        [AllowAnonymous]
        public string CheckReservation(RoomReservationModel bookingModel)
        {
            bool someRoomReserved = false;
            foreach(OneRoomReservationModel room in bookingModel.Rooms)
            {
                if (room.Reserved == true)
                {
                    someRoomReserved = true;
                    break;
                }
            }
            if (!someRoomReserved)
            {
                return "Err: At least one room should be reserved to proceed with the reservation.";
            }
            else if (bookingModel.StartDate >= bookingModel.EndDate)
            {
                return "Err: The end date should be at least one day after the start date. Please check the start date and end date.";
            }
            else if (!CheckRoomsAvailability(bookingModel))
            {
                return "Err: Some rooms are reserved during the selected period. Please check the start date and end date.";
            }
            else if (!CheckGuestsCount(bookingModel))
            {
                return "Err: The number of selected guests is greater than the reserved beds. Please check the reservation.";
            }
            else if (!User.Identity.IsAuthenticated)
            {
                return "Err: You should be authenticated to continue with the reservation. Please authenticate first.";
            }
            else
            {
                return "ok";
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult SaveReservationFormStatus(RoomReservationModel bookingModel)
        {
            if (bookingModel.Rooms != null)
            {
                ReservationOptions opt = new ReservationOptions();
                opt.ReservedRooms = new Dictionary<int, KeyValuePair<int, bool>>();
                opt.HotelId = bookingModel.HotelId;

                foreach (OneRoomReservationModel room in bookingModel.Rooms)
                {
                    if (room.SelectedBedCount == -1 && room.Reserved)
                    {
                        opt.ReservedRooms.Add(room.RoomId, new KeyValuePair<int, bool>(room.SelectedBedCount, true));
                    }
                    else
                    {
                        if (room.Reserved)
                        {
                            opt.ReservedRooms.Add(room.RoomId, new KeyValuePair<int, bool>(room.SelectedBedCount, true));
                        }
                        else
                        {
                            opt.ReservedRooms.Add(room.RoomId, new KeyValuePair<int, bool>(room.SelectedBedCount, false));
                        }
                    }
                }

                Session["ReservationOptions"] = opt;
            }
             
            return null;
        }

        [HttpPost]
        public ActionResult ReservationConfirmation(RoomReservationModel bookingModel)
        {
            ViewBag.Picture = HotelDAL.GetHotel(bookingModel.HotelId).HotelsRoomsPictures.FirstOrDefault();
            return View("ReserveConfirmation", bookingModel);
        }

        private bool CheckRoomsAvailability(RoomReservationModel bookingModel)
        {
            try
            {
                foreach (OneRoomReservationModel room in bookingModel.Rooms)
                {
                    if (room.Reserved)
                    {
                        ReservationsDAL.CheckRoomAvailability(false, room.RoomId, room.SelectedBedCount, bookingModel.StartDate,
                            bookingModel.EndDate);
                    }
                }
                return true;
            }
            catch (ReservationException ex)
            {
                return false;
            }
        }

        private bool CheckGuestsCount(RoomReservationModel bookingModel)
        {
            int countReservedBeds = 0;
            foreach (OneRoomReservationModel room in bookingModel.Rooms)
            {
                if (room.Reserved)
                {
                    if (room.SelectedBedCount == -1)
                    {
                        countReservedBeds += room.BedCount;
                    }
                    else
                    {
                        countReservedBeds += room.SelectedBedCount;
                    }
                }
            }
            return countReservedBeds >= bookingModel.GuestsCount;
        }

        [HttpPost]
        public ViewResult ConfirmReservation(RoomReservationModel reservationModel)
        {
            ViewBag.Picture = HotelDAL.GetHotel(reservationModel.HotelId).HotelsRoomsPictures.FirstOrDefault();
            if (ModelState.IsValid)
            {
                try
                {
                    foreach (OneRoomReservationModel room in reservationModel.Rooms)
                    {
                        if (room.Reserved)
                        {
                            var beds = room.SelectedBedCount == -1 ? room.BedCount : room.SelectedBedCount;
                            var user = AccountDAL.GetUser(User.Identity.Name);
                            var isHotelOwner = HotelDAL.GetHotel(reservationModel.HotelId).UserId == user.UserId;
                            ReservationsDAL.ReserveRoom(isHotelOwner, room.RoomId, User.Identity.Name, beds,
                                reservationModel.StartDate, reservationModel.EndDate);
                        }
                    }

                    ViewBag.Message = "Successful booking!";
                }
                catch (ReservationException ex)
                {
                    ViewBag.Message = ex.Message;
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Error while reserving. Please check your reservation.";
                }
            }
            else
            {
                ViewBag.Message = "Error while reserving. Please check your reservation.";
            }

            return View("ReserveConfirmation", reservationModel);
        }       
        
        [HttpGet]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult OwnerBooking(int hotelId, int roomId)
        {
            var hotel = HotelDAL.GetHotel(hotelId);

			ViewBag.Picture = HotelDAL.GetHotel(hotelId).HotelsRoomsPictures.FirstOrDefault();

            Room room;

            if (!hotel.Rooms.Any(r => r.RoomId == roomId))
                return HttpNotFound();

            room = hotel.Rooms.FirstOrDefault(r => r.RoomId == roomId);

            var bookingModel = new OwnerBookingModel(hotel.Name, hotel.HotelId, room.Name, room.Price, room.RoomType.Type, room.RoomId, room.BedCount,
                AccountDAL.GetUsers().Select(u => u.UserName).ToList());

            return View(bookingModel);
        }

        [HttpPost]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ViewResult OwnerBooking(OwnerBookingModel bookingModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
					ViewBag.Picture = HotelDAL.GetHotel(bookingModel.HotelId).HotelsRoomsPictures.FirstOrDefault();

                    // TODO: dormitory
                    ReservationsDAL.ReserveRoom(true, bookingModel.RoomId, bookingModel.SelectedUsername, bookingModel.BedCnt,
                        bookingModel.StartDate, bookingModel.EndDate);

                    ViewBag.Message = "Successful booking!";
					ViewBag.IsErr = false;
                }
                catch (ReservationException ex)
                {
                    ViewBag.Message = ex.Message;
					ViewBag.IsErr = true;
                }
                catch
                {
                    ViewBag.Message = "Error while reserving!";
					ViewBag.IsErr = true;
                }
            }

            return View(new OwnerBookingModel(bookingModel.HotelName, bookingModel.HotelId, bookingModel.RoomName, bookingModel.RoomPrice, bookingModel.RoomType,
					bookingModel.RoomId, bookingModel.BedCnt, AccountDAL.GetUsers().Select(u => u.UserName).ToList()));
        }
        

        [HttpGet]
        public ViewResult RoomBooking(int hotelId)
        {
            // bad id
            //if(hotelId<1) 
            var hotel = HotelDAL.GetHotel(hotelId);
            var rooms = hotel.Rooms
                             .Where(r => r.Price.HasValue)
                             .Select(r => new RoomModel(r.RoomId, r.Price.Value, r.BedCount))
                             .ToList();
            var bookingModel = new BookingModel(hotel.Name, hotel.HotelId, rooms);

            ViewBag.Picture = hotel.HotelsRoomsPictures.FirstOrDefault();
            
            return View(bookingModel);
        }
        
        [HttpPost]
        public ViewResult RoomBooking(BookingModel bookingModel)
        {
            if (bookingModel.HotelId > 0)
            {
                bookingModel.Rooms =
                    HotelDAL.GetHotel(bookingModel.HotelId).Rooms.Select(r => new RoomModel(r.RoomId,r.Price.Value,r.BedCount)).ToList();
            }

            ViewBag.Picture = HotelDAL.GetHotel(bookingModel.HotelId).HotelsRoomsPictures.FirstOrDefault();

            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: dormitory
                    //Reserve normal room
                    var beds = HotelDAL.GetHotel(bookingModel.HotelId).Rooms.First(r => r.RoomId == bookingModel.SelectedRoomId).BedCount;
                    var user = AccountDAL.GetUser(User.Identity.Name);
                    var isHotelOwner = HotelDAL.GetHotel(bookingModel.HotelId).UserId == user.UserId;
                    ReservationsDAL.ReserveRoom(isHotelOwner, bookingModel.SelectedRoomId, User.Identity.Name, beds, bookingModel.StartDate,
                        bookingModel.EndDate);
                    
                    ViewBag.Message = "Successful booking!";
					ViewBag.IsErr = false;
                }
                catch (ReservationException ex)
                {
                    ViewBag.Message = ex.Message;
					ViewBag.IsErr = true;
                }
                catch
                {
                    ViewBag.Message = "Error while reserving!";
					ViewBag.IsErr = true;
                }
            }
            
            return View(new BookingModel(bookingModel.HotelName, bookingModel.HotelId, bookingModel.Rooms));
        }
		#endregion

        #region room reservations
        [HttpGet]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult RoomReservations(int roomId)
        {
            return View(ReservationsDAL.GetRoomReservations(roomId));
        }

        [HttpPost]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult RoomReservations(int roomId, int reservationId, int newReservationStatusId)
        {
            ReservationsDAL.UpdateReservationStatusId(reservationId, newReservationStatusId);

            return View(ReservationsDAL.GetRoomReservations(roomId));
        }
        #endregion
        
        #region Hotels Create/Edit/Delete
        
        [HttpGet]
        //[Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult Create()
        {
            SetDafaultViewBags();
            return View();
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        //[Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult Create(HotelViewModel hotel)
        {
            if (ModelState.IsValid)
            {
                string creatorUserName = User.Identity.Name;
                var id = HotelDAL.AddHotel(hotel, AccountDAL.GetUser(creatorUserName).UserId);
                if (hotel.NewImage != null)
                {
                    hotel.HotelId = id; // add new identity id to add the image;
                    UploadImage(hotel);
                }
                return RedirectToAction("Index");
            }
            
            SetDafaultViewBags();
            return View(hotel);
        }
        
        [HttpGet]
        public ActionResult Edit(int id = 0)
        {
            if (IsOwnerOfTheHotel(id))
            {
                return RedirectToAction("MyHotels");
            }
            
            Hotel hotel = HotelDAL.GetHotel(id);
            if (hotel == null)
            {
                return HttpNotFound();
            }
            
            SetDafaultViewBags(hotel);
            return View(hotel.ToHotelViewModel());
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(HotelViewModel hotel)
        {
            if (IsOwnerOfTheHotel(hotel.HotelId))
            {
                return RedirectToAction("MyHotels");
            }
            
            if (ModelState.IsValid)
            {
                if (hotel.NewImage != null)
                {
                    UploadImage(hotel);
                }
                HotelDAL.EditHotel(hotel);
                return RedirectToAction("MyHotels");
            }
            
            SetDafaultViewBags();
            return View(hotel);
        }

        private void UploadImage(HotelViewModel model)
        {
            Bitmap original = Bitmap.FromStream(model.NewImage.InputStream) as Bitmap;
            if (original != null)
            {
                string imageFullName = String.Empty;
                var currentImage = HotelDAL.GetImage(model.HotelId);
                if (currentImage == null)
                {
                    string selectedImgName = Path.GetFileNameWithoutExtension(model.NewImage.FileName);
                    imageFullName = Server.MapPath("~/Pictures_Hotels/" + selectedImgName + ".jpg");
                    HotelDAL.AddImageToDB(selectedImgName + ".jpg", model.HotelId);
                    original.Save(imageFullName, ImageFormat.Jpeg);
                }
                else //overwrite old picture, and do not make db changes
                {
                    imageFullName = Server.MapPath("~/Pictures_Hotels/" + currentImage.PictureName);
                    original.Save(imageFullName, ImageFormat.Jpeg);
                }
            }
            else
            {
                ModelState.AddModelError("ImageFile",
                    "Your upload did not seem valid. Please try again using correct image!");
            }
        }
        
        [HttpGet]
        public ActionResult Delete(int id = 0)
        {
            if (IsOwnerOfTheHotel(id))
            {
                return RedirectToAction("MyHotels");
            }
            
            Hotel hotel = HotelDAL.GetHotel(id);
            if (hotel == null)
            {
                return HttpNotFound();
            }
            return View(hotel);
        }
        
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            if (IsOwnerOfTheHotel(id))
            {
                return RedirectToAction("MyHotels");
            }
            
            HotelDAL.RemoveHotel(id);
            return RedirectToAction("MyHotels");
        }

        #endregion

        #region Rooms Create/Edit/Delete

        [HttpGet]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult CreateRoom(int hotelId)
        {
            ViewBag.RoomTypeId = new SelectList(RoomDAL.GetRoomTypes(), "RoomTypeId", "Type");
            ViewBag.HotelId = hotelId;

            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult CreateRoom(RoomHotelModel room)
        {
            if (ModelState.IsValid)
            {
                HotelDAL.AddRoom(room);
                return RedirectToAction("ViewRooms", new { hotelId = room.HotelId });
            }

            ViewBag.RoomTypeId = new SelectList(RoomDAL.GetRoomTypes(), "RoomTypeId", "Type");
            return View(room);
        }

        [HttpGet]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult EditRoom(int roomId)
        {
            RoomHotelModel room = HotelDAL.GetRoomById(roomId);

            ViewBag.RoomTypeId = new SelectList(RoomDAL.GetRoomTypes(), "RoomTypeId", "Type");
            return View(room);
        }

        [HttpPost]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult EditRoom(RoomHotelModel room)
        {
            if (ModelState.IsValid)
            {
                HotelDAL.EditRoom(room);
                return RedirectToAction("ViewRooms", new { hotelId = room.HotelId });
            }

            ViewBag.RoomTypeId = new SelectList(RoomDAL.GetRoomTypes(), "RoomTypeId", "Type", room.RoomTypeId);
            return View(room);
        }

        [HttpGet]
        public ActionResult DeleteRoom(int roomId)
        {
            int hotelId = HotelDAL.GetRoomById(roomId).HotelId;
            HotelDAL.RemoveRoom(roomId);
            
            return RedirectToAction("ViewRooms", new { hotelId = hotelId });
        }

        [HttpGet]
        [Authorize(Roles = "Administrator, HotelOwner")]
        public ActionResult ViewRooms(int hotelId)
        {
            List<RoomHotelModel> rooms = HotelDAL.GetRoomsByHotelId(hotelId)
                .Select(a => new RoomHotelModel()
                {
                    RoomId = a.RoomId,
                    HotelId = a.HotelId,
                    Name = a.Name,
                    Price = a.Price,
                    Desciption = a.Description,
                    RoomType = new RoomTypeModel()
                    {
                        RoomTypeId = a.RoomType.RoomTypeId,
                        Type = a.RoomType.Type,
                        ReserveWhole = a.RoomType.ReserveWhole
                    },
                    BedCount = a.BedCount
                }).ToList();

            return View(rooms);
        }

        #endregion
        
        private void SetDafaultViewBags(Hotel h = null)
        {
            if (h != null)
            {
                ViewBag.AddressId = new SelectList(AddressDAL.GetAddresses(), "AddressId", "Region",
                    h.AddressId);
                ViewBag.HotelTypeId = new SelectList(HotelDAL.GetHotelTypes(), "HotelTypeId", "Type",
                    h.HotelTypeId);
                ViewBag.ReservationTypeId = new SelectList(ReservationsDAL.GetReservationTypes(),
                    "ReservationTypeId", "Type", h.ReservationTypeId);
                ViewBag.UserId = new SelectList(AccountDAL.GetUsers(), "UserId", "UserName", h.UserId);   
            }
            else
            {
                ViewBag.AddressId = new SelectList(AddressDAL.GetAddresses(), "AddressId", "Region");
                ViewBag.HotelTypeId = new SelectList(HotelDAL.GetHotelTypes(), "HotelTypeId", "Type");
                ViewBag.ReservationTypeId = new SelectList(ReservationsDAL.GetReservationTypes(),
                    "ReservationTypeId", "Type");
                ViewBag.UserId = new SelectList(AccountDAL.GetUsers(), "UserId", "UserName");   
            }
        }      

        private bool IsOwnerOfTheHotel(int id)
        {
            var user = User.Identity;
            if (user != null && !AccountDAL.GetUser(user.Name).IsOwnerOfTheHotel(id))
            {
                return true;
            }
            return false;
        }
    }
}